// pages/mine/payment/eletric/eletric.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,//当前tab
    type: 1,//1:电费 2:水费 3:气费 4:网费 5:物业费
    typeImg:"",//不同类型不同的图片
    status: 1,// 1: 未缴 2: 已缴 
    page: 1,
    listData:[],
    noMore: false,

    unpaidList:[],
    unpaidPage:1,
    unpaidNoMore:false,

    paidList:[],
    paidPage:1,
    paidNoMore:false,

    loading: true,

    parameter: "",//子页面传回的参数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({ status:options.status, type:options.type })
    
    //直接加载所有数据
    this.getUnpaidData(true,1)
    this.getPaidData(true,2)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    debugger
    if (this.data.parameter == "paySuccess"){
      
      //刷新。从第一页开始刷
      this.getUnpaidData(true, 1)
      this.getPaidData(true, 2)
    }
  },
  /**
   * 生命周期函数--监听页面渲染
   */
  onReady: function(){
    //设置title和图片
    this.setType(this.data.type)
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh(e) {
    console.log(this.data.status)
    switch (this.data.status){
      case "1":
        setTimeout(_ => {
          this.getUnpaidData(true,1)
        }, 1000)
        break;
      case "2":
        setTimeout(_ => {
          this.getPaidData(true,2)
        }, 1000)
        break;
    }
    
  },
  /**
   * 上拉加载更多
   */
  onReachBottom(e) {
    switch (this.data.status) {
      case "1":
        setTimeout(_ => {
          this.getUnpaidData(false,1)
        }, 1000)
        break;
      case "2":
        setTimeout(_ => {
          this.getPaidData(false,2)
        }, 1000)
        break;
    }
  },
  // 根据不同type设置不同的标题和图片
  setType(type){
    switch (type) {
      case "1":
        this.setData({ type: 1, typeImg: "/images/payment/eletric.png" })
        wx.setNavigationBarTitle({
          title: '电费',
        })
        break;
      case "2":
        this.setData({ type: 2, typeImg: "/images/payment/water.png" })
        wx.setNavigationBarTitle({
          title: '水费',
        })
        break;
      case "3":
        this.setData({ type: 3, typeImg: "/images/payment/gas.png" })
        wx.setNavigationBarTitle({
          title: '燃气费',
        })
        break;
      case "4":
        this.setData({ type: 4, typeImg: "/images/payment/net.png" })
        wx.setNavigationBarTitle({
          title: '网费',
        })
        break;
      case "5":
        this.setData({ type: 5, typeImg: "/images/payment/manage.png" })
        wx.setNavigationBarTitle({
          title: '物业费',
        })
        break;
    }
  },

  /**
  * 切换 顶级tabs
  */
  onChange(e) {
    this.setData({
      status: e.detail.index + 1,
    })
  },
 
  goToPage(e){
    let info = e.currentTarget.dataset
    wx.navigateTo({
      url: 'eletricDetail/eletricDetail?type=' + info.type + '&status=' + info.status + '&id=' + info.id,
    })
  },

  /**
   *获取未缴数据
   */
  getUnpaidData(init,statusIndex){
    if(init){
      this.setData({ unpaidPage:1, unpaidList:[], unpaidNoMore:false })
    }else{
      this.setData({ unpaidPage: ++ this.data.unpaidPage})
    }
    if(!this.data.unpaidNoMore){
      wx.showLoading({ title: '加载中' })
      this.setData({
        loading: true
      })
      app.globalData.request("post", "/applet/wx_pay/billList", {
        id: wx.getStorageSync("id"),
        token: wx.getStorageSync("token"),
        type: this.data.type,
        status: statusIndex,
        page: this.data.unpaidPage
      })
      .then(res => {
        if(this.data.unpaidPage == 1){
          this.setData({
            unpaidList: res.data.data,
            unpaidNoMore: res.data.no_more,
            loading: false
          })
        }else{
          this.setData({
            unpaidList: [...this.data.unpaidList,...res.data.data],
            unpaidNoMore: res.data.no_more,
            loading: false
          })
        }
        // 加载完毕后强制关闭下拉刷新
        wx.nextTick(() => {
          wx.hideLoading()
          wx.stopPullDownRefresh()
        })
      }).catch(err => {
        wx.showToast({
          title: err.msg,
        })
        this.setData({
          loading: false
        })
        wx.hideLoading()
      }) 
    }
  },
  /**
   * 获取已缴数据
   */
  getPaidData(init,statusIndex){
    if (init) {
      this.setData({ paidPage: 1, paidList: [], paidNoMore: false })
    } else {
      this.setData({ paidPage: ++this.data.paidPage })
    }
    if (!this.data.paidNoMore) {
      wx.showLoading({ title: '加载中' })
      this.setData({
        loading: true
      })
      app.globalData.request("post", "/applet/wx_pay/billList", {
        id: wx.getStorageSync("id"),
        token: wx.getStorageSync("token"),
        type: this.data.type,
        status: statusIndex,
        page: this.data.paidPage
      })
        .then(res => {
          if (this.data.paidPage == 1) {
            this.setData({
              paidList: res.data.data,
              paidNoMore: res.data.no_more,
              loading: false
            })
          } else {
            this.setData({
              paidList: [...this.data.paidList, ...res.data.data],
              paidNoMore: res.data.no_more,
              loading: false
            })
          }
          // 加载完毕后强制关闭下拉刷新
          wx.nextTick(() => {
            wx.hideLoading()
            wx.stopPullDownRefresh()
          })
        }).catch(err => {
          wx.showToast({
            title: err.msg,
          })
          this.setData({
            loading: false
          })
          wx.hideLoading()
        })
    }
  },
})